package com.example.projectsharedspace;

import java.util.ArrayList;

/**
 * Class for storing data regarding the inhabitants. (and more to come)
 * A Home can not have more than 10 (ten) inhabitants.
 * 
 * @author Jan Ove
 * @since 2013-24-10
 */
public class Home {

	/**
	 * The Id of the Home. Is given based on how many Homes there are in the database. 
	 * If there are 7 Homes, excluding this, the Id of this Home will be 7+1=8
	 */
	private int id;
	/** The name of the Home. Will be given by the User who creates it. */
	private String name;
	/** List of all the users that have been, and currently are living in the Home. */
	private ArrayList<UserInfo> users = new ArrayList<UserInfo>();
	/** The number of messages sent in the home. The starting number is based on the id of the Home. */
	private int msgCount;

	/** Inner class for keeping track of a User and his living situation. */
	private class UserInfo {
		User u;
		boolean livesHere;
	}
	
	/**
	 * Creates a new Home with the creator (User u) as the only inhabitant. Queries the database for an id-number.
	 * @param name the name of the Home.
	 * @param u the creator of the Home, and also the first inhabitant.
	 */
	public Home(String name, User u) {
		// TODO hente id fra database
		this.name = name;
		addUser(u);
		msgCount = (id*1000);
	}
	
	/**
	 * Adds a User to the Home.
	 * @param u the User to be added.
	 */
	public void addUser(User u) {
		UserInfo us = new UserInfo();
		us.u = u;
		us.livesHere = true;
		users.add(us);
	}
	
	/**
	 * Changes the living situation of the User in the users list.
	 * @param u the User to be moved out
	 */
	public void moveOutUser(User u) {
		int i = 0;
		while(u.getId() != users.get(i).u.getId()) {
			i++;
		}
		UserInfo us = users.get(i);
		us.livesHere = false;
		
		// Checks if everyone has moved out
		if(getPeopleLivingHere().size() == 0) {
			// TODO home is useless. clean up
		}
	}
	
	/**
	 * Removes the user from the Home completely. 
	 * This means null-values everywhere. If the user simply moved out, 
	 * use moveOutUser to change the value of his current living situation.
	 * 
	 * @param u the user to remove from the Home.
	 */
	public void removeUser(User u) {
		int i = 0;
		while(u.getId() != users.get(i).u.getId()) {
			i++;
		}
		users.remove(i);
		
		if(users.size() == 0) {
			// TODO clean up database. delete row etc
		}
	}
	
	/**
	 * @return the name of the Home.  
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @return the id of the Home.
	 */
	public int getId() {
		return id;
	}
	
	/**
	 * @return whether or not the data are consistent.
	 */
	public boolean dataInvariant() {
		boolean ok = true;
		ok &= name.length() > 3;
		assert ok : "Length of name is < 4. Length="+name.length();
		ok &= name.length() < 16;
		assert ok : "Length of name is > 15. Length="+name.length();
		ok &= id > 0;
		assert ok : "Id must be > 0. Id="+id;
		ok &= getPeopleLivingHere().size() < 11;
		assert ok : "Home can't have more than 10 inhabitants. Current inhabitants="+getPeopleLivingHere().size();
		for(UserInfo u : users) {
			ok &= u.livesHere && u.u.getHome() == this;
			assert ok : "User can not be having this as Home when not currently living here. " +
					"livesHere="+u.livesHere + " and Home="+u.u.getHome().getName();
		}
		for(UserInfo u : users) {
			ok &= !u.livesHere && u.u.getHome() != this;
			assert ok : "If a User is not living here, this can not be his home. " + 
					"livesHere="+u.livesHere + " and Home="+u.u.getHome();
		}
		return ok;
	}
	
	/**
	 * @return a list of the people currently living in the Home.
	 */
	public ArrayList<User> getPeopleLivingHere() {
		ArrayList<User> ret = new ArrayList<User>();
		for(int i = 0; i<users.size(); i++) {
			if(users.get(i).livesHere) {
				ret.add(users.get(i).u);
			}
		}
		return ret;
	}
	
	public void updateMsgCount() {
		msgCount++;
	}
	
	public int getMsgCount() {
		return msgCount;
	}
}
